import { defineStore } from 'pinia'
import { ref } from 'vue'
import { ElMessage } from 'element-plus'
import * as articleApi from '@/api/article'
import type { Article, ArticleCreateRequest, ArticleUpdateRequest } from '@/types/api'

export const useArticleStore = defineStore('article', () => {
  // 状态
  const articles = ref<Article[]>([])
  const currentArticle = ref<Article | null>(null)
  const loading = ref(false)
  const total = ref(0)
  const page = ref(1)
  const pageSize = ref(10)

  // 获取文章列表
  const fetchArticles = async (params?: {
    page?: number
    page_size?: number
    keyword?: string
  }) => {
    try {
      loading.value = true
      const response = await articleApi.getArticles(params)
      
      articles.value = response.articles
      total.value = response.total
      page.value = response.page
      pageSize.value = response.page_size
      
      return response
    } catch (error) {
      console.error('获取文章列表失败:', error)
      throw error
    } finally {
      loading.value = false
    }
  }

  // 获取文章详情
  const fetchArticle = async (id: number) => {
    try {
      loading.value = true
      const article = await articleApi.getArticle(id)
      currentArticle.value = article
      return article
    } catch (error) {
      console.error('获取文章详情失败:', error)
      throw error
    } finally {
      loading.value = false
    }
  }

  // 创建文章
  const createArticle = async (data: ArticleCreateRequest) => {
    try {
      loading.value = true
      const article = await articleApi.createArticle(data)
      articles.value.unshift(article)
      ElMessage.success('文章创建成功')
      return article
    } catch (error) {
      console.error('创建文章失败:', error)
      throw error
    } finally {
      loading.value = false
    }
  }

  // 更新文章
  const updateArticle = async (id: number, data: ArticleUpdateRequest) => {
    try {
      loading.value = true
      const article = await articleApi.updateArticle(id, data)
      
      // 更新列表中的文章
      const index = articles.value.findIndex(item => item.id === id)
      if (index !== -1) {
        articles.value[index] = article
      }
      
      // 更新当前文章
      if (currentArticle.value?.id === id) {
        currentArticle.value = article
      }
      
      ElMessage.success('文章更新成功')
      return article
    } catch (error) {
      console.error('更新文章失败:', error)
      throw error
    } finally {
      loading.value = false
    }
  }

  // 删除文章
  const deleteArticle = async (id: number) => {
    try {
      loading.value = true
      await articleApi.deleteArticle(id)
      
      // 从列表中移除
      articles.value = articles.value.filter(item => item.id !== id)
      
      // 清除当前文章
      if (currentArticle.value?.id === id) {
        currentArticle.value = null
      }
      
      ElMessage.success('文章删除成功')
    } catch (error) {
      console.error('删除文章失败:', error)
      throw error
    } finally {
      loading.value = false
    }
  }

  // 清除当前文章
  const clearCurrentArticle = () => {
    currentArticle.value = null
  }

  return {
    // 状态
    articles,
    currentArticle,
    loading,
    total,
    page,
    pageSize,
    // 方法
    fetchArticles,
    fetchArticle,
    createArticle,
    updateArticle,
    deleteArticle,
    clearCurrentArticle
  }
})
